package mining;

import java.io.IOException;

import stats.ArimaStats;
import stats.FpTreeStats;
import stats.RpTreeExt2Stats;
import stats.RpTreeInfoGainStats;
import stats.RpTreeStats;
import arima.Arima;
import arima.MrgExp;
import fpTree.FpTree;
import fpTree.RpTreeInfoGain;
import fpTree.RpTreeTooMany;
import fpTree.RpTree;

public class Compare {
	static Dataset l1 = new Dataset("./datasets/mock/mock.data.4.modified", 10);
	static Dataset l2 = new Dataset("./datasets/mock/mock.data.6.modified", 5);
	static Dataset l2a = new Dataset("./datasets/mock/mock.data.5.modified", 5);
	static Dataset l2b = new Dataset("./datasets/mock/mock.data.7.modified", 5);
	static Dataset l2c = new Dataset("./datasets/mock/mock.data.8.modified", 6);
	static Dataset l3 = new Dataset("./datasets/monk/monks.all.modified");
	static Dataset l4 = new Dataset("./datasets/monk/monks.red.modified");
	static Dataset l5 = new Dataset("./datasets/syn/T0.4L5.0N0.025.data.modified");
	static Dataset l6 = new Dataset("./datasets/syn/T0.3L5.0N0.025.data.modified");
	static Dataset l7 = new Dataset("./datasets/syn/T0.5L7.0N0.04.data.modified", 500);
	static Dataset l8 = new Dataset("./datasets/syn/T0.3L5.0N0.032.data.modified", 300);
	static Dataset l9 = new Dataset("./datasets/spect/SPECT.reduced.modified");	

	static Dataset l10 = new Dataset("./datasets/connect4/connect-4.data.modified", 67557);				// 67557, 9, 87
	static Dataset l11 = new Dataset("./datasets/voting_records/house-votes-84.data.modified", 435);	// 435, 17, 50
	static Dataset l12 = new Dataset("./datasets/spect/SPECT.all.modified", 267);						// 267, 23, 46
	static Dataset l13 = new Dataset("./datasets/primary_tumor/primary-tumor.data.modified", 339);		// 339, 18, 63
	static Dataset l14 = new Dataset("./datasets/zoo/zoo.data.modified", 101);							// 101, 18, 143
	static Dataset l15 = new Dataset("./datasets/tae/tae.data.modified", 63);							// 151, 6, 63
	static Dataset l16 = new Dataset("./datasets/flags/flags.data.modified", 194);						// 194, 29, 135
	static Dataset l17 = new Dataset("./datasets/adult/adult.all.modified", 48842);						// 48842, 11, 75

	static Dataset l18 = new Dataset("./datasets/dermatology/dermatology.data.modified", 366);			// 366, 34, 196
	static Dataset l19 = new Dataset("./datasets/soybean_large/soybean-large.data.modified", 307);		// 307, 36, 151
	static Dataset l20 = new Dataset("./datasets/mushroom/agaricus-lepiota.data.modified", 8124);		// 8124, 23, 119

	static Dataset l21 = new Dataset("./datasets/chess/kr-vs-kp.data.modified", 3196);					// 3196, 37, 75
	static Dataset l22 = new Dataset("./datasets/census/USCensus1990.data.modified", 2458285);			// 2458285, 68, 396

	public static void main(String[] args) throws IOException {										// transaction, items per trans, num items
//		Dataset[] datasets = new Dataset[]{l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l17,l18};
//		Dataset[] datasets = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};
//		double[] sups = new double[]{0.01,0.02,0.03,0.04,0.05};

//		Dataset[] datasets1 = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};		// RpTree
		Dataset[] datasets1 = new Dataset[]{l16};		// RpTree
		Dataset[] datasets1a = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};		// RpTreeInfoGain
//		Dataset[] datasets2 = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};		// FpTree
		Dataset[] datasets2 = new Dataset[]{l16};		// FpTree
//		Dataset[] datasets2 = new Dataset[]{l10};		// FP-Tree
//		Dataset[] datasets3 = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};		// MrgExp
		Dataset[] datasets3 = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};		// MrgExp
		Dataset[] datasets4 = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};		// Arima
		Dataset[] datasets5 = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};		// RpTreeTooMany
//		Dataset[] datasets6 = new Dataset[]{l10,l11,l12,l13,l14,l15,l16,l17};		// RpTreeExt
		Dataset[] datasets6 = new Dataset[]{l10};		// RpTreeExt
		Dataset[] datasets7 = new Dataset[]{l10};		// RpTreeExt2
		
//		System.out.println(RpTreeStats.headings());
////		double [] propRareItems = new double[]{.1,.2,.3,.4,.5,.6,.7,.8,.9,1};
//		double [] propRareItems = new double[]{.15};
//		for (Dataset l : datasets1)
//		{
//			int minRareItemsetSup = 5;
//			long runtime = 1;
//			int index = 0;
//			Thread t = new Thread(new Run_RpTree(l.location, minRareItemsetSup, (int) Math.ceil(l.transactions * propRareItems[0])));
////			runtime = run(t, 3600000); // 1 hr
//			runtime = run(t, 14400000); // 4 hr
//			index++;
//		}

//		System.out.println(RpTreeInfoGainStats.headings());
////		double [] propRareItems2 = new double[]{.1,.2,.3,.4,.5,.6,.7,.8,.9,1};
//		double [] propRareItems2 = new double[]{.15};
//		for (Dataset l : datasets1a)
//		{
//			int minRareItemsetSup = 5;
//			long runtime = 1;
//			int index = 0;
////			while (runtime > 0 && runtime < 30000 && index < propRareItems2.length) {
//				Thread t = new Thread(new Run_RpTreeInfoGain(l.location, 0.25, minRareItemsetSup, (int) Math.ceil(propRareItems2[0] * l.transactions)));
////				Thread t = new Thread(new Run_RpTreeInfoGain(l.location, propRareItems2[index], minRareItemsetSup, l.transactions + 1));
////				Thread t = new Thread(new Run_RpTreeInfoGain(l.location, propRareItems2[index], minRareItemsetSup, 4));
////				runtime = run(t, 3600000); // 1 hr
//				runtime = run(t, 14400000); // 4 hr
//				index++;
////			}
//		}
		
		System.out.println(FpTreeStats.headings());
		for (Dataset l : datasets2)
		{
			double[] sups = new double[]{0.15};
			int minRareItemsetSup = 20;
//			System.out.println("===== Dataset: \"" + l + "\" =====");
//			for (double sup : sups)
//			{
//				System.out.println("------------------");
				Thread t = new Thread(new Run_FpTree_Write(l.location, minRareItemsetSup, (int) Math.ceil(l.transactions * sups[0])));
//				Thread t = new Thread(new Run_FpTreeRare_Write(l.location, minRareItemsetSup, l.transactions + 1));
//				Thread t = new Thread(new Run_FpTreeRare_Write(l.location, minRareItemsetSup, 150));
//				run(t, 3600000); // 1 hr
				run(t, 14400000); // 4 hr
//			}
		}
		
//		System.out.println(MrgExpStats.headings());
//		for (Dataset l : datasets3)
//		{
//			double propMinFreqSup = 0.5;
//			int minRareItemsetSup = 1;
//			long runtime = 1;
////			System.out.println("===== Dataset: \"" + l + "\" =====");
//			while (runtime > 0 && runtime < 30000 && propMinFreqSup > 0.005) {
//				Thread t = new Thread(new Run_Arima_1stPart(l.location, minRareItemsetSup, (int) (l.transactions * propMinFreqSup)));
////				Thread t = new Thread(new Run_Arima_1stPart(l.location, minRareItemsetSup, 6000));
//				if (propMinFreqSup == 0.5)
//					runtime = run(t, 14400000);
//				else
//					runtime = run(t, 3600000);
//				propMinFreqSup /= 2;
//			}
//		}
		
//		System.out.println(ArimaStats.headings());
//		for (Dataset l : datasets4)
//		{
//			double propMinFreqSup = 0.15;
//			int minRareItemsetSup = 5;
//			
//			long runtime = 1;
////			System.out.println("===== Dataset: \"" + l + "\" =====");
////			while (runtime > 0 && runtime < 30000 && propMinFreqSup > 0.001) {
//				Thread t = new Thread(new Run_Arima_All(l.location, minRareItemsetSup, (int) Math.ceil(l.transactions * propMinFreqSup)));
//				runtime = run(t, 14400000); 
//				propMinFreqSup /= 2;
////				System.out.println("***************************************************************");
////			}
//		}

//		System.out.println(RpTreeTooManyStats.headings());
//		for (Dataset l : datasets5)
//		{
//			int minRareItemsetSup = 5;
//			double[] sups = new double[]{0.1,0.2,0.3,0.4,0.5,1};
//			long runtime = 1;
////			while (runtime > 0 && runtime < 600000) {
//				for (double sup : sups)
//				{
//					Thread t = new Thread(new Run_RpTreeTooMany(l.location, minRareItemsetSup, (int) (l.transactions * sup)));
//					runtime = run(t, 14400000);
//					if (runtime > (14400000 * 0.9))
//						break;
//				}
////			}
//		}
		
//		System.out.println(RpTreeExtStats.headings());
//		for (Dataset l : datasets6)
//		{
//			int minRareItemsetSup = 5;
//			double[] sups = new double[]{0.1,0.2,0.3,0.4,0.5,1};
//			long runtime = 1;
////			while (runtime > 0 && runtime < 600000) {
////				for (double sup : sups) {
////					Thread t = new Thread(new Run_RpTreeExt(l.location, minRareItemsetSup, (int) (l.transactions * sup)));
//					Thread t = new Thread(new Run_RpTreeExt(l.location, minRareItemsetSup, (int) (250)));
//					runtime = run(t, 14400000);
//					if (runtime > (14400000 * 0.9))
//						break;
////				}
////			}
//		}
		
//		System.out.println(RpTreeExt2Stats.headings());
//		for (Dataset l : datasets7)
//		{
//			int minRareItemsetSup = 5;
//			double[] sups = new double[]{0.1,0.2,0.3,0.4,0.5,1};
//			long runtime = 1;
////			while (runtime > 0 && runtime < 600000) {
////				for (double sup : sups) {
////					Thread t = new Thread(new Run_RpTreeExt_Test(l.location, minRareItemsetSup, (int) (l.transactions * sup)));
//					Thread t = new Thread(new Run_RpTreeExt2(l.location, minRareItemsetSup, (int) (250)));
//					runtime = run(t, 14400000);
//					if (runtime > (14400000 * 0.9))
//						break;
////				}
////			}
//		}

		//		Dataset l = l16;
//		for (int i : Arrays.asList(1)) {
//			System.out.println("---------------------------------------------------------");
////			System.out.println("minFreqSup: " + minFreqSup);
////			System.out.println("minFreqSup: " + i);
////			fpTree_Apriori_Test(location, i);
////			fpTree_Apriori_Test(location, minFreqSup);
//			fpTreeRareTest_Write(l.location, minRareSup, minFreqSup);
//			fpTree_YS(l.location, minRareSup, minFreqSup);
////			arima_Apriori_Test(location, 1, minFreqSup);
////			arima_1stPart_Test(location, 1, minFreqSup);
////			fpTreeRare_Apriori_Test(location, minRareSup, minFreqSup);
////			aprioriCustom(l, 1, 99999999);
//		}
		
		System.out.println("finished");
	}
	
	private static long run(Thread t, long timeout) {
		long t3 = System.nanoTime(), t4 = 0;
		t.start();
		try
		{
			t.join(timeout);
			if (t.isAlive()) {
				t.stop();
				System.out.println("*** Execution stopped: took longer than: " + timeout + " ms.");
			} else {
				t4 = System.nanoTime();
//				System.out.println("time taken: " + (t4-t3) / 1000000 + " ms.");
			}
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		
		return (t4-t3) / 1000000;
	}

	private static class Run_RpTree implements Runnable
	{
		RpTree fp;
//		RpTree_notWorking fp;
		private Run_RpTree(String location, int minRareItemsetSup, int minFreqItemsetSup)
		{
			this.fp = new RpTree(location);
			this.fp.setMinRareItemsetSupport(minRareItemsetSup);
			this.fp.setMinFreqItemsetSupport(minFreqItemsetSup);
		}
		
		public void run()
		{
			this.fp.run();
		}
	}

	private static class Run_RpTreeInfoGain implements Runnable
	{
		RpTreeInfoGain fp;
		private Run_RpTreeInfoGain(String location, double minInfoGain, int minRareItemsetSup, int minFreqItemsetSup)
		{
			this.fp = new RpTreeInfoGain(location, minInfoGain);
			this.fp.setMinRareItemsetSupport(minRareItemsetSup);
			this.fp.setMinFreqItemsetSupport(minFreqItemsetSup);
		}
		
		public void run()
		{
			this.fp.run();
		}
	}
	private static class Run_FpTree_Write implements Runnable
	{
		FpTree fpRare;
		private Run_FpTree_Write(String location, int minRareSup, int minFreqSup)
		{
			this.fpRare = new FpTree(location, true);
			this.fpRare.setMinRareItemsetSupport(minRareSup);
			this.fpRare.setMinFreqItemsetSupport(minFreqSup);
		}
		
		@Override
		public void run()
		{
			this.fpRare.run();
		}
		
	}
	
	private static class Run_Arima_1stPart implements Runnable
	{
		MrgExp a;
		
		private Run_Arima_1stPart(String location, int minRareSup, int minFreqSup)
		{
			this.a = new MrgExp(location);
			this.a.setMinFreqItemsetSupport(minFreqSup);
			this.a.setMinRareItemsetSupport(minRareSup);
		}

		public void run()
		{
			this.a.findMinGenerators();
		}
	}
	
	private static class Run_Arima_All implements Runnable
	{
		Arima a;
		
		private Run_Arima_All(String location, int minRareItemsetSup, int minFreqItemsetSup)
		{
			this.a = new Arima(location);
			this.a.setMinRareItemsetSupport(minRareItemsetSup);
			this.a.setMinFreqItemsetSupport(minFreqItemsetSup);
		}

		public void run()
		{
			this.a.run();
		}
	}
	
	private static class Run_RpTreeTooMany implements Runnable
	{
		RpTreeTooMany rpTree;
		
		private Run_RpTreeTooMany(String location, int minRareItemsetSup, int minFreqItemsetSup)
		{
			this.rpTree = new RpTreeTooMany(location);
			this.rpTree.setMinFreqItemsetSupport(minFreqItemsetSup);
			this.rpTree.setMinRareItemsetSupport(minRareItemsetSup);
		}

		public void run()
		{
			this.rpTree.run();
		}
	}

}
